﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace TDLib.Web
{
    /// <summary>
    /// 基本的页面类
    /// yangmeng(hvbird@gmail.com)
    /// 2009-09-12
    /// </summary>
    public class TDPage : System.Web.UI.Page
    {
        protected TDLib.Web.WebDebug m_debuger;

        /// <summary>
        /// 页面初始化，子面需要调用base.Page_Load才能保证执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            m_debuger = new TDLib.Web.WebDebug(this);
        }

        /// <summary>
        /// querystring数据转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="default_value"></param>
        /// <returns></returns>
        public static T GetQuery<T>(string key,T default_value)
        {
            T value = default_value;

            if (System.Web.HttpContext.Current.Request.QueryString[key] != null)
            {
                try
                {
                    value = (T)Convert.ChangeType(System.Web.HttpContext.Current.Request.QueryString[key].Trim(), typeof(T));
                }
                catch (System.Exception ex)
                {
                }
                
            }

            return value;
        }

        /// <summary>
        /// 接收form数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="default_value"></param>
        /// <returns></returns>
        public static T GetForm<T>(string key, T default_value)
        {
            T value = default_value;

            if (System.Web.HttpContext.Current.Request.Form[key] != null)
            {
                try
                {
                    value = (T)Convert.ChangeType(System.Web.HttpContext.Current.Request.Form[key].Trim(), typeof(T));
                }
                catch (System.Exception ex)
                {
                    value = default_value;
                }

            }

            return value;
        }
    }
}
